from selenium import webdriver
import time
from lxml import etree
import csv
import msvcrt
def get_driver(url):
	driver = webdriver.Chrome('/Program Files (x86)/Google/chromedriver.exe')
	driver.get(url)
	return driver


def get_page_source(driver, url):
	##函数睡眠1秒，等待网页响应和渲染
	time.sleep(1)
	page_source = driver.page_source
	return page_source

def get_xhtml(page_source):
	xhtml = etree.HTML(page_source)
	return xhtml

## 这是一个生成器函数
def parse_datas(xhtml, city):
	datas = {}
	items = xhtml.xpath('//article[@class="poi-item"]')
	for item in items:
		datas['city'] = city
		print(datas['city'])
		datas['hotel'] = item.xpath('.//a[@class="poi-title"]/text()')[0].strip()
		print(datas['hotel'])
		
		# print(item.xpath('.//div[@class="poi-price"]/em/text()')[0].strip())
		# datas['price'] = item.xpath('.//div[@class="poi-price"]/em/text()')[0].strip()
		
		## 部分酒店没有消费人数的信息，如果没有，返回空字符串
		try:
			datas['evaluation'] = item.xpath('.//span[@class="poi-grade-desc"]/text()')[0].strip()
			datas['grade'] = item.xpath('.//div[@class="poi-grade"]/text()')[0].strip()
			datas['consumer'] = item.xpath('.//div[@class="poi-buy-num"]/text()')[0].strip()
		except IndexError:
			datas['consumer'] = ''
		datas['address'] = item.xpath('.//div[@class="poi-address"]/text()')[0].strip()
		yield datas


def save_datas(datas):
	with open('hotel.csv', 'a', encoding='GB18030', newline='') as c:
		fieldnames = ['city', 'hotel', 'evaluation', 'grade', 'consumer', 'address','icon']
		writer = csv.DictWriter(c, fieldnames=fieldnames)
		for data in datas:
			writer.writerow(data)

if __name__ == '__main__':
	city = '德清'
	url  = 'https://hotel.meituan.com/deqing/'
	driver = get_driver(url)
	# xhtml = get_xhtml(page_source)
	for i in range(60):
		page_source = get_page_source(driver, url)
		xhtml = get_xhtml(page_source)
		datas = parse_datas(xhtml ,city)
		save_datas(datas)
		if ord(msvcrt.getch())==100:
			continue
		else:
			driver.quit()
			break